/**
 * 
 */
package neptune.web.server.repository;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import neptune.manager.builder.IPAddressRange;
import neptune.manager.model.topology.IPAddress;

/**
 * Gestiva la configurazione con file testuale
 * 
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * @deprecated
 */
public class ConfigurationFileRepository implements ConfigurationRepository {

	private String rootDir;
	private String clusterFile = "cluster.xml";
	private String topologyDir = "topologies";
	private boolean debug;
	private String experimentDir;

	public ConfigurationFileRepository(String path) {
		if (!path.endsWith(File.separator)) {
			path += File.separator;
		}
		rootDir = path;

		File confFile = new File(path + "emuPlugin.configuration");
		try {
			FileReader reader = new FileReader(confFile);
			BufferedReader breader = new BufferedReader(reader);
			String line = breader.readLine();

			while (line != null) {
				if (!line.startsWith("#") || !line.startsWith(" ")) {
					String[] row = line.split("=");
					if (row[0].equals("ClusterDescriptor"))
						clusterFile = row[1];
					else if (row[0].equals("TopologiesDir"))
						topologyDir = row[1];
					else if (row[0].equals("ExperimentsDir"))
						experimentDir = row[1];
					else if (row[0].equals("debug"))
						debug = Integer.parseInt(row[1]) == 1;
				}
				line = breader.readLine();
			}

		} catch (FileNotFoundException e) {
			// In caso di file non esistente, si adoperano le impostazioni di
			// default
			e.printStackTrace();
		} catch (IOException e) {
			// In caso di problemi di IO, si adoperano le impostazioni di
			// default
			e.printStackTrace();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeneptune.web.server.repository.ConfigurationRepository#
	 * getApplicationRootDirectory()
	 */
	public String getApplicationRootDirectoryPath() {
		return rootDir;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * neptune.web.server.repository.ConfigurationRepository#getClusterFilePath
	 * ()
	 */
	public String getClusterFilePath() {
		// TODO Auto-generated method stub
		return getApplicationRootDirectoryPath() + clusterFile;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeneptune.web.server.repository.ConfigurationRepository#
	 * getTopologyRootDirectoryName()
	 */
	public String getTopologyRootDirectoryName() {
		// TODO Auto-generated method stub
		return topologyDir;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeneptune.web.server.repository.ConfigurationRepository#
	 * getTopologyRootDirectoryPath()
	 */
	public String getTopologyRootDirectoryPath() {
		String add = "";
		if (!topologyDir.endsWith(File.separator))
			add = File.separator;

		return getApplicationRootDirectoryPath() + topologyDir + add;
	}

	public String getApplicationRootDirectoryName() {
		return rootDir;
	}

	public boolean debugActive() {
		return debug;
	}

	public String getExperimentsDirPath() {
		if (!experimentDir.endsWith(File.separator))
			experimentDir += File.separator;
		return rootDir + experimentDir;
	}

	public IPAddress getBaseExperimentSubnet() {
		try {
			// TODO Auto-generated method stub
			// TODO aggiungere i controlli sulla correttezza del formato
			// all'avvio del config repository
			return new IPAddress(4, "10.0.0.0", "255.255.240.0");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public IPAddressRange getControlIPRange() {
		// TODO Auto-generated method stub
		return null;
	}

	public IPAddress getClusterSubnet() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getVmTempRepDir() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getExperimentsVMsDir() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see neptune.web.server.repository.ConfigurationRepository#getLinkSubnet()
	 */
	public String getLinkSubnet() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see neptune.web.server.repository.ConfigurationRepository#getPathTemplate()
	 */
	public String getTemplatePath() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see neptune.web.server.repository.ConfigurationRepository#getConfigurationFilePath()
	 */
	public String getConfigurationFilePath() {
		// TODO Auto-generated method stub
		return null;
	}

}
